<?php

class AdminController extends BaseController
{
    public function __construct()
    {

    }

    public function getIndex()
    {
        return View::make('admin.index');
    }

    public function postIndex()
    {
        echo "string";
    }

    public function getLeague()
    {

        $resultAllLeague = AdminLeague::getAllLeague();


        return View::make('admin.league-manage')->with('allLeague', $resultAllLeague);
    }

    public function getLeagueAdd()
    {
        return View::Make('admin.league-add');
    }

    public function postLeagueAdd()
    {
        $input = Input::all();

        $rule = array('lname' => 'required',
            'limage_file' => 'required'
        );

        $messages = array(
            'lname.required' => 'Tên giải đấu không được rỗng.',
            'limage_file.required' => 'Chưa chọn hình ảnh.'
        );

        $valid = Validator::make($input, $rule, $messages);

        if ($valid->passes()) {
            $lname = $input['lname'];
            $ldescription = $input['ldescription'];
            $file = Input::file('limage_file');

            $destinationPath = 'assets/img/league/' . str_random(8);
            $file_name = $file->getClientOriginalName();
            $image_full = $destinationPath . '/' . $file_name;
        } else {
            return Redirect::to('admin/league-add')->withInput()->withErrors($valid);
        }

        $uploadSuccess = $file->move($destinationPath, $file_name); //upload to server folder

        if ($uploadSuccess) {
            $result = AdminLeague::addLeague($lname, $ldescription, $image_full);

            if ($result != null) {
                return Redirect::to('admin/league');
            } else {
                return Redirect::to('fuck');
            }
        } else {
            return Redirect::to('fuck');
        }
    }

    public function postLeagueDelete()
    {
        $input = Input::all();

        $id = $input['id'];

        $result = AdminLeague::deleteLeagueById($id);

        if ($result != null)
            $resultAllLeague = AdminLeague::getAllLeague();

        return Redirect::to('admin/league');

    }

    public function getLeagueModify()
    {

        $input = Input::all();

        $id = $input['id'];

        $resultLeagueById = AdminLeague::getLeagueById($id);

        return View::make('admin.league-modify')->with('leagueById', $resultLeagueById);
    }

    /*public function postLeagueModify(){

        $input = Input::all();

        $id = $input['id'];

        $resultLeagueById = AdminLeague::getLeagueById($id);

        return View::make('admin.league-modify')->with('leagueById', $resultLeagueById);
    }*/

    public function postLeagueModify()
    {

        $input = Input::all();

        $lid = $input['id'];
        $lname = $input['lname'];
        $ldescription = $input['ldescription'];
        $file = Input::file('limage_file');

        if ($file != null) {
            $destinationPath = 'assets/img/league/' . str_random(8);
            $file_name = $file->getClientOriginalName();
            $image_full = $destinationPath . '/' . $file_name;

            $uploadSuccess = $file->move($destinationPath, $file_name); //upload to server folder

            if ($uploadSuccess) {
                $result = AdminLeague::modifyLeague($lid, $lname, $ldescription, $image_full);

                if ($result != null) {
                    return Redirect::to('admin/league');
                } else {
                    return Redirect::to('fuck');
                }
            } else {
                return Redirect::to('fuck');
            }
        } else {

            $image_full = '';
            $result = AdminLeague::modifyLeague($lid, $lname, $ldescription, $image_full);

            if ($result != null) {
                return Redirect::to('admin/league');
            } else {
                return Redirect::to('fuck');
            }
        }


    }


    public function getSport()
    {

        $resultAllSport = AdminSport::getAllSport();


        return View::make('admin.sport-manage')->with('allSport', $resultAllSport);
    }


    public function getSportAdd()
    {
        return View::make('admin.sport-add');
    }


    public function postSportAdd()
    {
        $input = Input::all();

        $rule = array('sname' => 'required',
            'simage_file' => 'required'
        );

        $messages = array(
            'sname.required' => 'Tên giải đấu không được rỗng.',
            'simage_file.required' => 'Chưa chọn hình ảnh.'
        );

        $valid = Validator::make($input, $rule, $messages);

        if ($valid->passes()) {
            $sname = $input['sname'];
            $sdescription = $input['sdescription'];
            $file = Input::file('simage_file');

            $destinationPath = 'assets/img/sport/' . str_random(8);
            $file_name = $file->getClientOriginalName();
            $image_full = $destinationPath . '/' . $file_name;
        } else {
            return Redirect::to('admin/sport-add')->withInput()->withErrors($valid);
        }

        $uploadSuccess = $file->move($destinationPath, $file_name); //upload to server folder

        if ($uploadSuccess) {
            $result = AdminSport::addSport($sname, $sdescription, $image_full);

            if ($result != null) {
                return Redirect::to('admin/sport');
            } else {
                return Redirect::to('fuck');
            }
        } else {
            return Redirect::to('fuck');
        }
    }

    public function postSportDelete()
    {
        $input = Input::all();

        $id = $input['id'];

        $result = AdminSport::deleteSportById($id);

        if ($result != null)
            $resultAllSport = AdminSport::getAllSport();

        return Redirect::to('admin/sport');

    }


    public function getSportModify()
    {

        $input = Input::all();

        $id = $input['id'];

        $resultSportById = AdminSport::getSportById($id);

        return View::make('admin.sport-modify')->with('sportById', $resultSportById);
    }



    public function postSportModify()
    {

        $input = Input::all();

        $sid = $input['id'];
        $sname = $input['sname'];
        $sdescription = $input['sdescription'];
        $file = Input::file('simage_file');

        if ($file != null) {
            $destinationPath = 'assets/img/league/' . str_random(8);
            $file_name = $file->getClientOriginalName();
            $image_full = $destinationPath . '/' . $file_name;

            $uploadSuccess = $file->move($destinationPath, $file_name); //upload to server folder

            if ($uploadSuccess) {
                $result = AdminSport::modifySport($sid, $sname, $sdescription, $image_full);

                if ($result != null) {
                    return Redirect::to('admin/sport');
                } else {
                    return Redirect::to('fuck');
                }
            } else {
                return Redirect::to('fuck');
            }
        } else {

            $image_full = '';
            $result = AdminLeague::modifyLeague($sid, $sname, $sdescription, $image_full);

            if ($result != null) {
                return Redirect::to('admin/sport');
            } else {
                return Redirect::to('fuck');
            }
        }


    }




    public function getTeam()
    {

        $resultAllTeam = AdminTeam::getAllTeam();


        return View::make('admin.team-manage')->with('allTeam', $resultAllTeam);
    }

    public function getTeamAdd()
    {
        return View::Make('admin.team-add');
    }

    public function postTeamAdd()
    {
        $input = Input::all();

        $rule = array('tname' => 'required',
            'timage_file' => 'required'
        );

        $messages = array(
            'tname.required' => 'Tên giải đấu không được rỗng.',
            'timage_file.required' => 'Chưa chọn hình ảnh.'
        );

        $valid = Validator::make($input, $rule, $messages);

        if ($valid->passes()) {
            $lname = $input['tname'];
            $ldescription = $input['tdescription'];
            $file = Input::file('timage_file');

            $destinationPath = 'assets/img/team/' . str_random(8);
            $file_name = $file->getClientOriginalName();
            $image_full = $destinationPath . '/' . $file_name;
        } else {
            return Redirect::to('admin/team-add')->withInput()->withErrors($valid);
        }

        $uploadSuccess = $file->move($destinationPath, $file_name); //upload to server folder

        if ($uploadSuccess) {
            $result = AdminTeam::addTeam($lname, $ldescription, $image_full);

            if ($result != null) {
                return Redirect::to('admin/team');
            } else {
                return Redirect::to('fuck');
            }
        } else {
            return Redirect::to('fuck');
        }
    }

    public function postTeamDelete()
    {
        $input = Input::all();

        $id = $input['id'];

        $result = AdminTeam::deleteTeamById($id);

        if ($result != null)
            $resultAllLeague = AdminTeam::getAllTeam();

        return Redirect::to('admin/team');

    }

    public function getTeamModify()
    {

        $input = Input::all();

        $id = $input['id'];

        $resultTeamById = AdminTeam::getTeamById($id);

        return View::make('admin.team-modify')->with('teamById', $resultTeamById);
    }

    /*public function postLeagueModify(){

        $input = Input::all();

        $id = $input['id'];

        $resultLeagueById = AdminLeague::getLeagueById($id);

        return View::make('admin.league-modify')->with('leagueById', $resultLeagueById);
    }*/

    public function postTeamModify()
    {

        $input = Input::all();

        $tid = $input['id'];
        $tname = $input['tname'];
        $tdescription = $input['tdescription'];
        $file = Input::file('timage_file');

        if ($file != null) {
            $destinationPath = 'assets/img/team/' . str_random(8);
            $file_name = $file->getClientOriginalName();
            $image_full = $destinationPath . '/' . $file_name;

            $uploadSuccess = $file->move($destinationPath, $file_name); //upload to server folder

            if ($uploadSuccess) {
                $result = AdminTeam::modifyTeam($tid, $tname, $tdescription, $image_full);

                if ($result != null) {
                    return Redirect::to('admin/team');
                } else {
                    return Redirect::to('fuck');
                }
            } else {
                return Redirect::to('fuck');
            }
        } else {

            $image_full = '';
            $result = AdminTeam::modifyTeam($tid, $tname, $tdescription, $image_full);

            if ($result != null) {
                return Redirect::to('admin/team');
            } else {
                return Redirect::to('fuck');
            }
        }


    }

}
